﻿using System;
using System.Windows.Forms;
using TeacherHelp.FormList;


namespace TeacherHelp.FormList.Admin
{
    public partial class UserAddForm : BanFlashingForm
    {
        private readonly AdminMainForm _parentForm;

        private UserAddForm()
        {
            InitializeComponent();
        }



        public UserAddForm(AdminMainForm parent) : this()
        {
            _parentForm = parent;
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            ViewModel.User user = new ViewModel.User();

            user.LoginName = this.loginNameTxt.Text.Trim();
            user.LoginPwd = this.pwdTxt.Text.Trim();
            string pwdOk = this.pwdOkTxt.Text.Trim();
            user.Name = this.nameTxt.Text.Trim();
            user.Sex = this.boyradio.Checked ? "男" : "女";
            user.RoleId = this.typeComBox.SelectedValue.ToInt();
            user.gradeId = this.groupComBox.SelectedValue.ToInt();
            user.roleName = this.typeComBox.Text.Trim();

            if (string.IsNullOrEmpty(user.LoginName))
            {
                MessageBox.Show("用户姓名不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(user.LoginPwd))
            {
                MessageBox.Show("密码不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (user.LoginPwd != pwdOk)
            {
                MessageBox.Show("两次输入密码不一致！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(user.Name))
            {
                MessageBox.Show("姓名不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            BLL.UserManager userManager = new BLL.UserManager();
            string msg = userManager.AddUser(user);

            var isExit = MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK;

            if (isExit)
            {
                _parentForm.ShowUserInfoForm();
            }

        }

        private void UserAddForm_Load(object sender, EventArgs e)
        {
            //类型下拉框初始化
            typeComboxDataInit();
            //班级下拉框初始化
            groupComboxDataInit();
        }

        private void groupComboxDataInit()
        {
            BLL.GradeManager gradeManager = new BLL.GradeManager();
            var data = gradeManager.GetGradeAllList();
            this.groupComBox.DataSource = data;
            this.groupComBox.DisplayMember = nameof(Model.Grade.Name);
            this.groupComBox.ValueMember = nameof(Model.Grade.Id);
        }

        private void typeComboxDataInit()
        {
            BLL.RoleManager roleManager = new BLL.RoleManager();
            var data = roleManager.GetRoleAllList();
            this.typeComBox.DataSource = data;
            this.typeComBox.DisplayMember = nameof(Model.Role.Name);
            this.typeComBox.ValueMember = nameof(Model.Role.Id);
            //throw new NotImplementedException();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            _parentForm.ShowUserInfoForm();
        }
    }
}
